package DB;

import java.util.Vector;

import QueryHandler.Predicate;


public class Selection extends RAQuery {

	private Vector<Predicate> pred;

/*
 * Constructor
 */
	
	public Selection(RAQuery raq, Vector<Predicate> pred) {
		this.pred = pred;
		this.raQuery = raq;
	}

/*
 * @see DB.RAQuery#compute()
 * 
 * In selection, the Probability doesn't change in the
 * relevant tuples.
 */
	
	@Override
	public Table compute() {
		Table t = raQuery.compute();
		Table ans = new Table("", t.getKey(), t.getTitles(), t.getTotalProb());
		if (pred.size() == 0){
			ans = t;
		}
		else{
			for(int i = 0; i < t.getSize(); i++){
				for (int j = 0; j < pred.size(); j++){
					if(pred.elementAt(j).checkPred(t, t.getTuple(i))){
						ans.addTuple(t.getTuple(i), t.getTupleProp(i));
						//System.out.println("adding: " + t.getTuple(i).elementAt(0) + "with probability: " + t.getTupleProp(i));
					}
				}
			}
		}
		return ans;	
	}

}
